package edu.kit.csl.cratyli.control;

import java.util.Map;

import edu.kit.csl.cratyli.datatypes.BadParameterValueException;
import edu.kit.csl.cratyli.datatypes.Word;
import edu.kit.csl.cratyli.ui.Configuration;
import edu.kit.csl.cratyli.ui.Logger;

/**
 * An instance of this class implements a merging strategy. Implementing
 * subclasses should use the annealing function to weight between the
 * primary and secondary words.
 */
public abstract class Merger {

	protected AnnealingFunction annealing = null;
	
	/**
	 * Sole constructor. Set up the annealing function according the 
	 * configuration parameters.
	 */
	public Merger() throws BadParameterValueException {
		String ann = Configuration.getSingleton()
				.getString("annealingFunction");
		if (ann.equals("constant")) {
			annealing = new ConstantAnnealingFunction();
		} else if (ann.equals("root")) {
			annealing = new RootAnnealingFunction();
		} else {
			throw new BadParameterValueException(
					"Annealing function not known");
		}
	}
	
	/**
	 * Calls next() of the annealing function.
	 */
	public void next() {
		annealing.next();
		Logger.getSingleton().notice("Temperature is now "
				+ annealing.getTemperature());
	}
	
	/**
	 * Merges the primary word with a set of secondary words. and writes the
	 * result directly to the given dictionary.
	 * 
	 * @param pWord the primary word (must be present in dict)
	 * @param sWords secondary words with their confidences. The confidences
	 * 			should sum up to 1. All words must be present in dict
	 * @throws NullPointerException if one of the arguments is null.
	 */
	public abstract void merge(Word pWord, Map<Word, Double> sWords);
}
